<?php

/**
 * 通用操作
 * @author pwstrick
 */
namespace bc\mongo;
use yii\mongodb\ActiveRecord;
class Mongo extends ActiveRecord {
	
	/**
	 * 插入
	 */
	public function add($data, $options=array()) {
		$collection = $this->getCollection();
		return $collection->save($data, $options);
	}
	
	/**
	 * mongo使用封装了的query查询数量
	 * @return int
	 */
	public function getCounts($where = array(), $field = null, $distinct = FALSE) {
		$collection = $this->getCollection();
		if($distinct && !empty($field)) {
			$rows = $collection->distinct($field, $where);
			$count = count($rows);
		}else {
			$count = $collection->find($where)->count();
		}
		return $count;
	}
	
	/**
	 * mongo通用查询过滤
	 * @return array
	 */
	private function _setFilter($where, $order, $fields, $count = 0, $offset = 0) {
		$collection = $this->getCollection();
		$find = $collection->find($where);
	
		if(!empty($fields)) {
			$newFields = array();
			foreach ($fields as $value) {
				$newFields[$value] = 1;
			}
			$find = $find->fields($newFields);
		}
		if(!empty($order)) {
			$find = $find->sort($order);
		}
		if($count > 0) {
			$find = $find->skip($offset)->limit($count);
		}
		$rows = $this->setFindList($find);
		return $rows;
	}
	
	/**
	 * 将mongo根据find查询出的信息转换为array
	 */
	protected function setFindList($cursor) {
		$attrs = array();
		foreach ($cursor as $entity_data) {
			$attrs[] = $entity_data;
		}
		return $attrs;
	}
	
	/**
	 * mongo根据_id查询一条记录
	 * @return array
	 */
	public function getRowById($id, $fields = null) {
		$where = array('_id' => new \MongoId($id));
		$rows = $this->_setFilter($where, null, $fields);
		if(empty($rows))
			return $rows;
		return $rows[0];
	}
	
	/**
	 * mongo根据_id数组查询多条条记录
	 * @return array
	 */
	public function getRowsByIds($ids, $fields = null) {
		if(empty($ids)) return;
		$mids = array();
		foreach ($ids as $id) {
			$mids[] = new \MongoId($id);
		}
		$where = array('_id' => array('$in' => $mids));
		$rows = $this->_setFilter($where, null, $fields);
		return $rows;
	}
	
	/**
	 * mongo查询一条记录
	 * @return array
	 */
	public function getRow($where = array(), $order = null, $fields = null) {
		$rows = $this->_setFilter($where, $order, $fields);
		if(empty($rows))
			return $rows;
		return $rows[0];
	}
	
	/**
	 * mongo查询多条信息
	 */
	public function getRows($where = array(), $order = null, $count = 0, $offset = 0, $fields = null) {
		$rows = $this->_setFilter($where, $order, $fields, $count, $offset);
		return $rows;
	}
	
	/**
	 * mongo分页获取多条信息 快捷方式
	 */
	public function getRowsLimitByPage($where = array(), $order = null, $page = 1, $size = 10, $fields = null) {
		return $this->mgetRows($where, $order, $size, ($page - 1) * $size, $fields);
	}
	
	/**
	 * @return boolean
	 */
	public function mupdate($row, $where = array(), $inc = array()) {
		$collection = $this->getCollection();
		$new_object = array('$set' => $row);
		if(!empty($inc)) {
			$new_object['$inc'] = $inc;
		}
		$affected = $collection->update($where, $new_object, array("multiple" => true, 'w'=>1));
		print_r($affected);
		return $affected > 0;
	}
	
	/**
	 * @return boolean
	 */
	public function updateById($row, $id) {
		$collection = $this->getCollection();
		$where = array('_id' => new \MongoId($id));
		return $this->mupdate($row, $where);
	}
	
	/**
	 * @return int 返回受影响行数
	 */
	public function mdelete($where) {
		$collection = $this->getCollection();
		$affected = $collection->remove($where);
		return (int)$affected['n'];
	}
}